var rowClass = '';
var itemViewWindow;

var librarySupportObj = {	
	toggleAddEditForm : function() {
		if ($('formAddEditItem').style.display == '') {
			/*
			 * Close form
			 */
			$('buttonNewItem').style.display = '';
			this.clearAddEditForm();
			new Effect.SlideUp('formAddEditItem', {direction: 'top', duration: .2});
		} else {
			/*
			 * Open form
			 */
			$('buttonNewItem').style.display = 'none';
			new Effect.SlideDown('formAddEditItem', {direction: 'top', duration: .2});
		}
	},
	clearAddEditForm : function() {
		$('formAction').value = 'add';
		$('lCat').options[0].selected = true;
		$('lISBN').value              = '';
		$('lTitle').value             = '';
		$('lAuthors').value           = '';
		tinyMCE.getInstanceById('lAbstract').getBody().innerHTML = '';	
		$('lPublisher').value         = '';
		$('tags').value               = '';
		$('lPublisher').value         = '';
		$('lFormat').options[1].selected = true;
		$('lInHouseOnly').checked = false;
		$('lLangEnglish').checked  = true;
		$('lLangSpanish').checked  = false;
		$('lLangRussian').checked  = false;
		
		/*
		 * Reset title
		 */
		var updateTabObject = mainTabs.getItem('pageTab1');
		updateTabObject.setTitle('Update Library');		
	},
	toggleAdvancedSearchForm : function() {
		if ($('formAdvancedSearch').style.display == '') {
			/*
			 * Close form
			 */
			$('formSimpleSearch').style.display = '';
			this.clearAdvancedSearchForm();
			new Effect.SlideUp('formAdvancedSearch', {direction: 'top', duration: .2});
		} else {
			/*
			 * Open form
			 */
			$('formSimpleSearch').style.display = 'none';
			new Effect.SlideDown('formAdvancedSearch', {direction: 'top', duration: .2});
		}
	},
	clearAdvancedSearchForm : function() {
		
	},
	createItemViewWindow : function() {
		/*
		 * Create library item view window
		 */
		if(!itemViewWindow){
            itemViewWindow = new Ext.Window({
				el:          'itemViewWindow',
                id:          'itemViewWindow',
                width:       550,
                height:      350,
                resizable:   true,
                closeAction: 'hide',
                plain:       true,
                title:       '<b>Library Item Viewer</b>',
                shadow:      false,
				autoScroll:  true
            });            
        }		
        itemViewWindow.show();
        return itemViewWindow;	
	}
}

var getLibraryDataObj = {
	getLibraryItems : function(start,limit,sort,acdc,paging,searchString,renderTo) {
		$('libraryLogStart').value = start;
		var params = 'handler=getLibraryItemsForGrid&start='+start+'&limit='+limit+'&sort='+sort+'&acdc='+acdc+'&paging='+paging+'&searchString='+searchString+'&renderTo='+renderTo;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				if (transport.responseText == '0') {
					$('messageSearch').className = 'errorMessageBig';
					$('messageSearch').innerHTML = 'Your search did not return any results.';
					$('messageSearch').style.display = '';
					$('libraryAdvancedSearchContainer').innerHTML = '';
				} else {
					libraryRenderObj.renderLibraryRows(transport.responseText);
					$('messageSearch').style.display = 'none';
				}	
			},
			onFailure: function() {
				alert('Oops!');
			}			
		});	
	},
	getLibraryItem : function(purpose,libraryID) {
		var params = 'handler=getLibraryItem&libraryID='+libraryID+'&purpose='+purpose;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				if (purpose == 'edit') {
					libraryUpdateObj.renderEditForm(transport.responseText);
				} else if (purpose == 'window') {
					libraryRenderObj.renderLibraryDetail('window',transport.responseText);
				}
			},
			onFailure: function() {
				alert('Oops!');
			}			
		});
	},
	getLibraryItemsOut : function(viewport,start,limit,sort,acdc,paging,memberID) {
		var params = 'handler=getLibraryItemsOut&start='+start+'&limit='+limit+'&sort='+sort+'&acdc='+acdc+'&paging='+paging+'&memberID='+memberID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				if (transport.responseText == '0') {
					$('libraryListCheckedOut').innerHTML = '<p><b>No items checked out at this time.</b></p>';
				} else {
					libraryRenderObj.renderLibraryItemsOut(transport.responseText,viewport);
				}				
			},
			onFailure: function(response) {					
				logErrorAJAX(response);
			}
		});		
	}
}

var librarySearchObj = {
	autocompleterFieldsLibrary : function() {
		searchLibraryStore = new Ext.data.Store({
		    proxy: new Ext.data.HttpProxy({
				url: 'ajaxDirector.php?handler=getLibraryItemsForAutocompleter'
		    }),
			    reader: new Ext.data.JsonReader({
	            root: 'Library',
	            totalProperty: 'Total',
	            id: 'LibraryID'
	        }, [
	            {name: 'Title', mapping: 'Title'},
	            {name: 'LibraryID', mapping: 'LibraryID'}
	        ])
    	});
    	var resultTpl = new Ext.XTemplate(
	        '<tpl for="."><div class="search-item">',
	            '<span>{Title}</span>',
	        '</div></tpl>'
	    );    
	    var search = new Ext.form.ComboBox({
	        store:        searchLibraryStore,
	        displayField: 'Title',
	        typeAhead:    false,
	        loadingText:  'Searching...',
	        width:        380,
	        hideTrigger:  true,
	        tpl:          resultTpl,
	        applyTo:      'librarySearchBox',
	        itemSelector: 'div.search-item',
	        pageSize:10,
	        resizable:     true,
	        shadow:        false,
	        grow:          true,
	        growMax:       500,
	        growMin:       300,
	        listWidth:     380,
	        handleHeight:  12,
	        onSelect: function(record){ 
	        	libraryRenderObj.toggleItemView(record.data.LibraryID);
	        }
	    });	
	},
	advancedLibrarySearch : function() {
		/*
		 * Cobble together a search string
		 */
		var keyword  = $F('sKeyword');
		var isbn     = $F('sISBN');
		var category = $F('sCat'); 
				
		if (
			keyword  == '' &&
			isbn     == '' &&
			category == ''
			) {
			var searchString = '';			
		} else {		
			var searchString = keyword+'|'+isbn+'|'+category;
		}	        		                                 
		getLibraryDataObj.getLibraryItems(0,20,0,0,0,searchString,'libraryAdvancedSearchContainer');
	}
}

var libraryUpdateObj = {
	saveLibraryItem : function() {

          var lAbstract  = tinyMCE.get('lAbstract').getContent();
		var formAction = $F('formAction');
		var dataString = {
			action:      formAction,
			libraryID:   $F('libraryID'),
			catID:       $F('lCat'),
			isbn:        $F('lISBN'),
			title:       $F('lTitle'),
			authors:     $F('lAuthors'),
			lAbstract:   lAbstract,
		     publishers:  $F('lPublisher'),
		     format:      $F('lFormat'),
		     tags:        $F('tags'),
		     inHouse:     $F('lInHouseOnly'),
		     langEnglish: $F('lLangEnglish'),
		     langSpanish: $F('lLangSpanish'),
		     langRussian: $F('lLangRussian')
		}
		jsonString = Object.toJSON(dataString);
          var reqHash = {
                         'handler'    : 'saveLibraryItem',
                         'jsonString' : jsonString
                         };
          var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: reqHash,
			onSuccess: function(transport) {
				if (rowClass == '') {
					rowClass = 'altRow';
				} else {
					rowClass = '';
				}
				updateStatus('Library item saved!','success','messageLibrarySave');
				var jsonString = transport.responseText;
				if (formAction == 'add') {
					libraryRenderObj.renderLibraryRow(jsonString,'libraryListNewItems',rowClass);
				}
			}
		});
	},
	renderEditForm : function(stringJSON) {
		/*
		 * If we're not in the Update Library pane, put us there
		 */
		librarySupportObj.clearAddEditForm();
		
		var jsonObject = stringJSON.evalJSON();
		
		var libraryID   = jsonObject.LibraryID;
		var isbn        = jsonObject.ISBN;
		var title       = jsonObject.Title;
		var author      = jsonObject.Authors;
		var catID       = jsonObject.CatID;
		var lAbstract   = jsonObject.Abstract;
		var langEnglish = jsonObject.LanguageEnglish;
		var langSpanish = jsonObject.LanguageSpanish;
		var langRussian = jsonObject.LanguageRussian;
		var formatID    = jsonObject.FormatID;
		var tags        = jsonObject.Tags;
		var publisher   = jsonObject.Publisher;
		var inHouse     = jsonObject.InhouseViewingOnly;
		
		var updateTabObject = mainTabs.getItem('pageTab1');
		updateTabObject.setTitle('Editing: '+title);	
				
		$('formAction').value = 'edit';
		$('libraryID').value  = libraryID;
		
		/*
		 * Category select
		 */
		var optionsCat = $('lCat').options;
		for(a=0;a<optionsCat.length;a++) {
			if ($('lCat').options[a].value == catID) {
				$('lCat').options[a].selected = true;
			}
		}
		/*
		 * Format select
		 */
		var optionsFormat = $('lFormat').options;
		for(a=0;a<optionsFormat.length;a++) {
			if ($('lFormat').options[a].value == formatID) {
				$('lFormat').options[a].selected = true;
			}
		}
		
		$('lISBN').value      = isbn;
		$('lTitle').value     = title;
		$('lAuthors').value   = author;
		tinyMCE.getInstanceById('lAbstract').getBody().innerHTML = lAbstract;
		$('lPublisher').value = publisher;
		$('tags').value       = tags;
		if (inHouse == 1) {
			$('lInHouseOnly').checked = true;
		}
		$('lLangEnglish').checked = false;
		$('lLangSpanish').checked = false;
		$('lLangRussian').checked = false;
		if (langEnglish == 1) {
			$('lLangEnglish').checked = true;
		}
		if (langSpanish == 1) {
			$('lLangSpanish').checked = true;
		}
		if (langRussian == 1) {
			$('lLangRussian').checked = true;
		}
		
		new SwappableTags("tags");
		mainTabs.setActiveTab('pageTab1');		
	},
	returnDeleteLibraryItem : function(libraryID) {
		Effect.Fade('container'+libraryID, {delay: .1, duration: 1});
	}
}

var libraryRenderObj = {
	renderLibraryRows : function(stringJSON) {
		var jsonObject = stringJSON.evalJSON();
		
		var totalItems    = jsonObject.TotalRows;
		var returnedRows  = ((jsonObject.Items.length)-1);
		var renderTo      = jsonObject.RenderTo;
		var totalPages    = jsonObject.TotalPages;
		var start         = parseInt(jsonObject.Start);
		var limit         = parseInt(jsonObject.Limit);
		var sort          = jsonObject.Sort;
		var acdc          = jsonObject.AcDc;
		var origAcDc      = jsonObject.OrigAcDc;
		var currentPage   = jsonObject.CurrentPage;
		var searchString  = jsonObject.SearchString;
		var resultsTitle  = jsonObject.ResultsTitle;
		
		var prevStart = parseInt(start-limit);
		var nextStart = parseInt(start+limit);
		
		var navTab = '<table style="margin: 6px 0 0 5px;">' +
						 '<tr><td style="width: 17px; padding: 3px;">';
		if (prevStart<0) {
			navTab += '<img src="images/button_prev_off.gif" />';
		} else {
			navTab += '<a href="#" onClick="getLibraryDataObj.getLibraryItems('+prevStart+','+limit+',\''+sort+'\',\''+origAcDc+'\',0,\''+searchString+'\',\''+renderTo+'\'); return false;" title="Previous Page">' +
					  '<img src="images/button_prev.gif" style="border: none;" alt="Previous Page"></a>';
		}
			navTab += '</td>';
			
		if (totalPages>1) {
			navTab += '<td>';
			navTab += '<select id="navPages" size="1" onChange="getLibraryDataObj.getLibraryItems(this.options[this.selectedIndex].value,'+limit+',\''+sort+'\',\''+origAcDc+'\',0,\''+searchString+'\',\''+renderTo+'\'); return false;" style="width: 50px;">';
			for(a=1;a<=totalPages;a++) {
				var value = parseInt(a*20)-20;
				navTab += '<option value="'+value+'"';
				if (currentPage == a) {
					navTab += ' selected ';
				}
				navTab += '>'+a+'</option>';
			}
			navTab += '</select>';
			navTab += '</td>';
		}
			
			navTab += '<td style="width: 17px; padding: 3px;">';
		if (nextStart>totalItems) {
			navTab += '<img src="images/button_next_off.gif" />';
		} else {
			navTab += '<a href="#" onClick="getLibraryDataObj.getLibraryItems('+nextStart+','+limit+',\''+sort+'\',\''+origAcDc+'\',0,\''+searchString+'\',\''+renderTo+'\'); return false;" title="Next Page">' +
					  '<img src="images/button_next.gif" style="border: none;" alt="Next Page"></a>';
		}			 
			navTab += '</td>';
			navTab += '</tr></table>';
					
		if (totalItems>0) {
			/*
			 * Clear search results container
			 */
			$(renderTo).innerHTML = '';
						
			var headerContainer = document.createElement('div');
			headerContainer.setAttribute('id','searchResultsHeader');
			
			var headerOut  = '<div id="resultsTitleContainer" style="float: left;"><h1 id="resultsTitle">'+resultsTitle+'</h1></div>';
				headerOut += '<div style="float: right; margin-right: 5px;"><div id="gridNavTab" class="gridNavTab" style="display: none;"></div></div>';
				headerOut += '<div style="clear: both;"></div>';
			
			headerContainer.innerHTML = headerOut;
			$(renderTo).appendChild(headerContainer);
			
			$('gridNavTab').innerHTML = navTab;	
			$('gridNavTab').style.display = '';
			
			/*
			 * Create table title
			 */
			
			var rowContainer = document.createElement('div');
			rowContainer.setAttribute('id','rowHeader');
			
			var stringOut = '<table class="dataTable" style="font-size: 1em;">';
			stringOut    += '<tr>';
			stringOut    += '<th style="width: 5%;"></th>';
			stringOut    +=	'<th onmouseover="this.className=\'thHover\';" onmouseout="this.className=\'\';" onClick="getLibraryDataObj.getLibraryItems('+start+','+limit+',\'Title\',\''+acdc+'\',0,\''+searchString+'\',\''+renderTo+'\'); return false;" style="width: 45%;"><div ext:qtip="Sort by Title" style="cursor: pointer;">Title</div></th>' + 
							'<th onmouseover="this.className=\'thHover\';" onmouseout="this.className=\'\';" onClick="getLibraryDataObj.getLibraryItems('+start+','+limit+',\'Authors\',\''+acdc+'\',0,\''+searchString+'\',\''+renderTo+'\'); return false;" style="width: 45%;"><div ext:qtip="Sort by Author" style="cursor: pointer;">Author</div></th>';
			stringOut    += '</tr></table>';	
			
			rowContainer.innerHTML = stringOut;
			$(renderTo).appendChild(rowContainer);		
			
			for(a=0;a<=returnedRows;a++) {
				if ((a % 2) > 0) { 
					rowClass = 'altRow'; 
				} else { 
					rowClass = '';
				}
				this.renderLibraryRow(jsonObject.Items[a],renderTo,rowClass);
			}
		} else {
			
		}
	},
	renderLibraryRow : function(stringJSON,container,rowClass) {
		if (typeof(stringJSON) != 'object') {
			var jsonObject = stringJSON.evalJSON();
		} else {
			var jsonObject = stringJSON;
		}		
		
		var libraryID = jsonObject.LibraryID;
		var title     = jsonObject.Title;
		var author    = jsonObject.Authors;	

		var rowContainer = document.createElement('div');
		rowContainer.setAttribute('class','dataTable');
		rowContainer.setAttribute('id','container'+libraryID);
		
		var stringOut  = '<div class="'+rowClass+' itemRowView" id="viewRow'+libraryID+'"><div style="padding: 5px; float: left; text-align: center; width: 5%;">';
			stringOut += ' <a href="#" onClick="trashUpdateObj.sendToTrash(\''+libraryID+'\',\'library\'); return false;" title="Delete this libraryItem"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete" /></a>';
			stringOut += ' <a href="#" onClick="getLibraryDataObj.getLibraryItem(\'edit\','+libraryID+'); return false;" title="Edit this library item"><img src="images/buttonEditBig.gif" style="border: none;" alt="Edit" /></a></td>';
			stringOut += '</div>';	
			stringOut += '<div style="float: left; width: 45%;"><p><a href="#" onClick="libraryRenderObj.toggleItemView('+libraryID+');">'+title+'</a></p></div>';
			stringOut += '<div style="float: left; width: 45%;"><p>'+author+'</p></div>';
			stringOut += '<div style="clear: left;"></div></div>';
			stringOut += '<div id="viewDetail'+libraryID+'" class="blueBox itemDetailView" style="padding: 10px; display: none;"></div>';
		
		rowContainer.innerHTML = stringOut;
		$(container).appendChild(rowContainer);		
	},
	renderLibraryDetail : function(purpose,stringJSON) {
		var jsonObject = stringJSON.evalJSON();
		
		var libraryID   = jsonObject.LibraryID;
		var title       = jsonObject.Title;
		var author      = jsonObject.Authors;	
		var category    = jsonObject.Category;
		var lAbstract   = jsonObject.Abstract;
		var format      = jsonObject.Format;
		if (jsonObject.linkAmazon != null) {
			var linkAmazon  = jsonObject.linkAmazon[0];			
		}	
		if (jsonObject.imageAmazon != null) {
			var imageAmazon = jsonObject.imageAmazon[0];
		}
		var publisher   = jsonObject.Publisher;
		var langEnglish = jsonObject.LanguageEnglish;
		var langSpanish = jsonObject.LanguageSpanish;
		var langRussian = jsonObject.LanguageRussian;
		var inHouseView = jsonObject.InhouseViewingOnly;
		var tags        = jsonObject.Tags;
		
			var outString = '<div style="float: left; margin-right: 10px;">';
			if (purpose == 'row') {
				outString += ' <a href="#" onClick="trashUpdateObj.sendToTrash(\''+libraryID+'\',\'library\'); return false;" title="Delete this libraryItem"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete" /></a>';
				outString += ' <a href="#" onClick="getLibraryDataObj.getLibraryItem(\'edit\','+libraryID+'); return false;" title="Edit this library item"><img src="images/buttonEditBig.gif" style="border: none;" alt="Edit" /></a></td>';
			}
			outString += '</div><div style="float: left;">';
			outString += '<h2>'+title+'</h2>';
			outString += '<p><b><i>'+author+'</i></b></p>';
			outString += '</div><div style="clear: left;"></div>';
			if (imageAmazon) {
				outString += '<div class="imgLeft"><a href="'+linkAmazon+'" target="_blank"><img src="'+imageAmazon+'" class="imgBorder" title="'+title+'" alt="'+title+'" /></a></div>';
			}
			outString += '<p>'+lAbstract+'</p>';
			outString += '<div style="clear: left;"></div>';
			outString += '<p>'+tags+'</p>';
		if (purpose == 'row') {
			$('viewDetail'+libraryID).innerHTML = outString;	
		} else if (purpose == 'window') {
			librarySupportObj.createItemViewWindow();
			var windowString = '<div style="padding: 5px; background: #fff; height: 100%;"><p>'+outString+'</div>';
			itemViewWindow.body.update(windowString);
		}		
	},
	revertAllToRowView : function() {
		var itemRows   = document.getElementsByClassName('itemRowView');
		var itemPanels = document.getElementsByClassName('itemDetailView');
		
		for(i = 0; i < itemRows.length; i++) {
			/*
			 * Revert any open panels back to rows
			 */
			itemRows[i].style.display   = '';
			itemPanels[i].style.display = 'none';
		}
	},
	toggleItemView : function(libraryID) {
		var viewRow    = $('viewRow'+libraryID);
		var viewDetail = $('viewDetail'+libraryID);
		
		if (viewRow == null) {
			/*
			 * Then we're coming from an autocompleter click
			 */
			$('libraryAdvancedSearchContainer').innerHTML = '';
			
			var rowContainer = document.createElement('div');
			rowContainer.setAttribute('id','viewRow'+libraryID);
			
			var detailContainer = document.createElement('div');
			detailContainer.setAttribute('id','viewDetail'+libraryID);
			detailContainer.setAttribute('class','blueBox itemDetailView');
			detailContainer.setAttribute('style','padding: 10px;');
			
			$('libraryAdvancedSearchContainer').appendChild(rowContainer);
			$('libraryAdvancedSearchContainer').appendChild(detailContainer);
			
			var viewRow    = $('viewRow'+libraryID);
			var viewDetail = $('viewDetail'+libraryID);
		}
		
		this.revertAllToRowView();
		if (viewRow.style.display == '') {
			viewRow.style.display    = 'none';
			viewDetail.style.display = '';
			
			var params = 'handler=getLibraryItem&libraryID='+libraryID;
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,
				onSuccess: function(transport) {
					libraryRenderObj.renderLibraryDetail('row',transport.responseText);
				},
				onFailure: function(response) {					
					logErrorAJAX(response);
				}
			});			
		} else {
			viewRow.style.display    = '';
			viewDetail.style.display = 'none';					
		}
	},	
	renderLibraryItemsOut : function(stringJSON,viewport) {
		var jsonObject = stringJSON.evalJSON();
		var totalItems    = jsonObject.TotalRows;
		var returnedRows  = ((jsonObject.Items.length)-1);
		var sort          = jsonObject.Sort;
		var acdc          = jsonObject.AcDc;
		var origAcDc      = jsonObject.OrigAcDc;
		var resultsTitle  = jsonObject.ResultsTitle;
		
		/*
		 * Replace page title
		 */
		if ($('libraryCheckoutTitle')) {
			$('libraryCheckoutTitle').innerHTML = resultsTitle;
		}	
		
		var start;
		var limit;
		
		if (totalItems>0) {
			var stringOut = '<table class="dataTable" style="font-size: 1em;">';
			stringOut    += '<tr>';
			if (viewport == 'page') {
				stringOut    += '<th style="width: 5%;"></th>';
			}	
			
			stringOut    +=	'<th onmouseover="this.className=\'thHover\';" onmouseout="this.className=\'\';" onClick="getContactDataObj.getContactsForGrid(\'page\','+start+','+limit+',\'C.IDNumber\',\''+acdc+'\',0); return false;" style="width: 25%;"><div ext:qtip="Sort by book Title" style="cursor: pointer;">Title</div></th>' + 
							'<th onmouseover="this.className=\'thHover\';" onmouseout="this.className=\'\';" onClick="getContactDataObj.getContactsForGrid(\'page\','+start+','+limit+',\'C.SubjectLine\',\''+acdc+'\',0); return false;" style="width: 25%;"><div ext:qtip="Sort by Name" style="cursor: pointer;">Name</div></th>' +
							'<th onmouseover="this.className=\'thHover\';" onmouseout="this.className=\'\';" onClick="getContactDataObj.getContactsForGrid(\'page\','+start+','+limit+',\'S.StaffLName\',\''+acdc+'\',0); return false;" style="width: 20%;"><div ext:qtip="Sort by Date Out" style="cursor: pointer;">Date Out</div></th>' +
							'<th onmouseover="this.className=\'thHover\';" onmouseout="this.className=\'\';" onClick="getContactDataObj.getContactsForGrid(\'page\','+start+','+limit+',\'S.StaffLName\',\''+acdc+'\',0); return false;" style="width: 20%;"><div ext:qtip="Sort by Date In" style="cursor: pointer;">Date In</div></th>' +
							'<th onmouseover="this.className=\'thHover\';" onmouseout="this.className=\'\';" onClick="getContactDataObj.getContactsForGrid(\'page\','+start+','+limit+',\'S.StaffLName\',\''+acdc+'\',0); return false;" style="width: 5%;"><div ext:qtip="Sort by Status" style="cursor: pointer;">Status</div></th>';
			for(a=0;a<=returnedRows;a++) {
				var libraryID      = jsonObject.Items[a].LibraryID;
				var title          = jsonObject.Items[a].Title;
				var authors        = jsonObject.Items[a].Authors;
				var memberID       = jsonObject.Items[a].MemberID;
				var firstName      = jsonObject.Items[a].FirstName;
				var lastName       = jsonObject.Items[a].LastName;
				var fullName       = jsonObject.Items[a].BorrowerName;
				var checkOutID     = jsonObject.Items[a].CheckedOutID;
				var numberLent     = jsonObject.Items[a].NumberLent;
				var dateLent       = jsonObject.Items[a].DateLent;
				var numberReturned = jsonObject.Items[a].NumberReturned;
				var dateReturned   = jsonObject.Items[a].DateReturned;
				var statusDiv      = jsonObject.Items[a].Status;
				var doNotLend      = jsonObject.Items[a].DoNotLend;
								
				if ((a % 2) > 0) { 
					var rowClass = 'altRow'; 
				} else { 
					var rowClass = '';
				}			
				var rowID = libraryID+memberID;
				var memberName = firstName+' '+lastName;
				stringOut += '<tr id="row'+rowID+'" class="'+rowClass+'">';
				if (viewport == 'page') {
					stringOut += '<td style="text-align: center;">';
					stringOut += ' <a href="#" onClick="alert(\'Not ready yet!\'); return false;" title="Close this contact"><img src="images/buttonCloseBig.gif" style="border: none;" alt="Close this contact" /></a></td>';
				}
				stringOut += '<td>'+title+'</td>';
				if (doNotLend == 1) {
					stringOut += '<td class="statusAlertRow" ext:qtip="Do Not Lend!"><a href="#" onClick="" title="View member information"><img src="images/silkIcons/vcard.png" style="border: none;" /></a> <img src="images/silkIcons/delete.png"> <a href="#" onClick="getLibraryDataObj.getLibraryItemsOut(\'page\',0,20,\'DateLent\',\'DESC\',0,'+memberID+'); return false;">'+memberName+'</a></td>';
				} else {
					stringOut += '<td><a href="#" onClick="" title="View member information"><img src="images/silkIcons/vcard.png" style="border: none;" /></a> <a href="#" onClick="getLibraryDataObj.getLibraryItemsOut(\'page\',0,20,\'DateLent\',\'DESC\',0,'+memberID+'); return false;">'+memberName+'</a></td>';
				}
				stringOut += '<td>'+dateLent+'</td>';
				stringOut += '<td>'+dateReturned+'</td>';
				stringOut += '<td>'+statusDiv+'</td>';
				stringOut += '</tr>';			
				
			}					
			stringOut += '</table>';
			$('libraryListCheckedOut').innerHTML = stringOut;			
		} else {
			$('libraryListCheckedOut').innerHTML = 'No items checked out at this time.';
		}
	}
}


